package com.demo.chapter5stream2;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.demo.domain.Trader;
import com.demo.domain.Transaction2;

public class PuttingIntoPractice {

	public static void main(String[] args) {
		Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario","Milan");
        Trader alan = new Trader("Alan","Cambridge");
        Trader brian = new Trader("Brian","Cambridge");

		List<Transaction2> transactions = Arrays.asList(
            new Transaction2(brian, 2011, 300),
            new Transaction2(raoul, 2012, 1000),
            new Transaction2(raoul, 2011, 400),
            new Transaction2(mario, 2012, 710),
            new Transaction2(mario, 2012, 700),
            new Transaction2(alan, 2012, 950)
        );

		// Query 1: Find all transactions from year 2011 and sort them by value (small to high).
        List<Transaction2> tr2011 = transactions.stream()
                                               .filter(transaction -> transaction.getYear() == 2011)
                                               .sorted(Comparator.comparing(Transaction2::getValue))
                                               .collect(Collectors.toList());

        // Query 2: What are all the unique cities where the traders work?
        List<String> cities =
            transactions.stream()
                        .map(transaction -> transaction.getTrader().getCity())
                        .distinct()
                        .collect(Collectors.toList());
        System.out.println(cities);

        // Query 3: Find all traders from Cambridge and sort them by name.
        List<Trader> traders =
            transactions.stream()
                        .map(Transaction2::getTrader)
                        .filter(trader -> trader.getCity().equals("Cambridge"))
                        .distinct()
                        .sorted(Comparator.comparing(Trader::getName))
                        .collect(Collectors.toList());
        System.out.println(traders);

        // Query 4: Return a string of all traders’ names sorted alphabetically.
        String traderStr =
            transactions.stream()
                        .map(transaction -> transaction.getTrader().getName())
                        .distinct()
                        .sorted()
                        .collect(Collectors.joining());
        System.out.println(traderStr);

        // Query 5: Are there any trader based in Milan?
        boolean milanBased =
            transactions.stream()
                        .anyMatch(transaction -> transaction.getTrader()
                                                            .getCity()
                                                            .equals("Milan")
                                 );
        System.out.println(milanBased);

        // Query 6: Update all transactions so that the traders from Milan are set to Cambridge.
        transactions.stream()
                    .map(Transaction2::getTrader)
                    .filter(trader -> trader.getCity().equals("Milan"))
                    .forEach(trader -> trader.setCity("Cambridge"));
        System.out.println(transactions);

        // Query 7: What's the highest value in all the transactions?
        int highestValue =
            transactions.stream()
                        .map(Transaction2::getValue)
                        .reduce(0, Integer::max);
        System.out.println(highestValue);

        Optional<Transaction2> highestValue2 =
                transactions.stream()
                            .min(Comparator.comparing(Transaction2::getValue));
	}

}
